package com.platform.service.impl;

import com.platform.dao.LotteryRecordDao;
import com.platform.dao.UserAssetDao;
import com.platform.entity.*;
import com.platform.service.EnergySpcService;
import com.platform.service.LotteryRecordService;
import com.platform.service.UserService;
import com.platform.util.CoinTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 抽奖记录Service实现类
 *
 * @author liweihai
 * @email liweihai12345@sina.com
 * @date 2019-04-16 19:32:12
 */
@Service("lotteryRecordService")
public class LotteryRecordServiceImpl implements LotteryRecordService {
    @Autowired
    private LotteryRecordDao lotteryRecordDao;
    @Autowired
    private UserService userService;
    @Autowired
    private UserAssetDao userAssetDao;
    @Autowired
    private EnergySpcService energySpcService;

    @Override
    public int savesignrecordinfo(LotteryEntity lotteryEntity , Long uid, BigDecimal pirce,BigDecimal spend) {
        if(savesignrecord(lotteryEntity,uid,pirce,spend))
            return 1;
        else
            return 0;
    }

    @Transactional
    public  boolean savesignrecord(LotteryEntity lotteryEntity , Long uid, BigDecimal pirce,BigDecimal spend)
    {
        UserEntity user = userService.queryObject(uid);

        UserAssetEntity asset = userAssetDao.queryObjectForUid(uid);
        asset.setSpcCoin(asset.getSpcCoin().add(pirce));
        int i = userAssetDao.updateAsset(asset);
        if (i <= 0) {
            return false;
        }
        EnergySpcEntity energySpcEntity = new EnergySpcEntity();
        energySpcEntity.setUid(user.getId());
        energySpcEntity.setInviteUid(user.getId());
        energySpcEntity.setPhone(user.getMobile());
        energySpcEntity.setInvitePhone(user.getMobile());
        energySpcEntity.setImage(user.getPictureUrl());
        energySpcEntity.setType(CoinTypeEnum.COIN_TYPE_GAME_IN.getKey());
        energySpcEntity.setIncomeType(0);
        energySpcEntity.setTypeName(CoinTypeEnum.COIN_TYPE_GAME_IN.getValue());
        energySpcEntity.setEnergyCoin(pirce.doubleValue());
        energySpcEntity.setCreateDate(new Date());
        energySpcService.save(energySpcEntity);

        LotteryRecordEntity lotteryRecordEntity = new  LotteryRecordEntity();
        lotteryRecordEntity.setUid(uid);
        lotteryRecordEntity.setName(user.getName());
        lotteryRecordEntity.setPhone(user.getMobile());
        lotteryRecordEntity.setLotteryId(lotteryEntity.getId());
        lotteryRecordEntity.setLotteryName(lotteryEntity.getName());
        lotteryRecordEntity.setLevelName(lotteryEntity.getLevelName());
        lotteryRecordEntity.setLevel(lotteryEntity.getLevel());
        lotteryRecordEntity.setSpend(spend);
        lotteryRecordEntity.setType(4);
        lotteryRecordEntity.setCreateDate(new Date());
        lotteryRecordEntity.setPrice(pirce);
        lotteryRecordDao.save(lotteryRecordEntity);

        return true;
    }

    @Override
    public LotteryRecordEntity queryObject(Integer id) {
        return lotteryRecordDao.queryObject(id);
    }

    @Override
    public List<LotteryRecordEntity> queryList(Map<String, Object> map) {
        return lotteryRecordDao.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return lotteryRecordDao.queryTotal(map);
    }

    @Override
    public int save(LotteryRecordEntity lotteryRecord) {
        return lotteryRecordDao.save(lotteryRecord);
    }

    @Override
    public int update(LotteryRecordEntity lotteryRecord) {
        return lotteryRecordDao.update(lotteryRecord);
    }

    @Override
    public int delete(Integer id) {
        return lotteryRecordDao.delete(id);
    }

    @Override
    public int deleteBatch(Integer[] ids) {
        return lotteryRecordDao.deleteBatch(ids);
    }

    @Override
    public BigDecimal queryLotterySendTotal() {
        return lotteryRecordDao.queryLotterySendTotal();
    }

    @Override
    public BigDecimal queryLotteryPriceTotal() {
        return lotteryRecordDao.queryLotteryPriceTotal();
    }
}
